﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace FileNameCheck
{
    public class FileNameCheckHelper
    {
        public string RootPath;
        public int CheckDepth;
        public string[] StartWith;
        public string[] EndWith;
        public string[] Ignore;
        public string[] TextureTypeEnd;
        public string[] TextureType;

        public bool CheckFolder = false;

        protected List<string> unMatchPath = new List<string>();

        public FileNameCheckHelper(string r, int d, string s, string e, string ig,string texT,string texE)
        {
            RootPath = r;
            CheckDepth = d;
            string[] temp = s.Split(';');
            if (temp == null || temp.Length <= 0)
            {
                StartWith = new string[] { s };
            }
            else
            {
                StartWith = temp;
            }

            temp = e.Split(';');
            if (temp == null || temp.Length <= 0)
            {
                EndWith = new string[] { e };
            }
            else
            {
                EndWith = temp;
            }

            temp = ig.Split(';');
            if (temp == null || temp.Length <= 0)
            {
                Ignore = new string[] { ig };
            }
            else
            {
                Ignore = temp;
            }

            temp = texT.Split(';');
            if (temp == null || temp.Length <= 0)
            {
                TextureType = new string[] { texT };
            }
            else
            {
                TextureType = temp;
            }

            temp = texE.Split(';');
            if (temp == null || temp.Length <= 0)
            {
                TextureTypeEnd = new string[] { texE };
            }
            else
            {
                TextureTypeEnd = temp;
            }
        }

        public List<string> Check()
        {
            GetFolders(RootPath, CheckDepth);

            return unMatchPath;

        }

        public void GetFolders(string folderPath, int depth)
        {
            depth--;
            DirectoryInfo parentFolder = new DirectoryInfo(folderPath);
            DirectoryInfo[] folderes = parentFolder.GetDirectories();
            FileInfo[] files = parentFolder.GetFiles();

            foreach (FileInfo file in files)
            {
                if(IsNoCheck(file.Name))
                {
                    continue;
                }

                string[] temp = file.Name.Split('.');
                if (Array.IndexOf(TextureType, temp[1].ToLower()) >= 0)
                {
                    TextureCheck tex = new TextureCheck(file.FullName);

                    if(!NameInRuleTexture((int)tex.Type,temp[0]))
                    {
                        unMatchPath.Add(file.FullName);
                    }
                    continue;
                }
                if (!NameInRule(temp[0]))
                {
                    unMatchPath.Add(file.FullName);
                }
            }


            foreach (DirectoryInfo file in folderes)
            {
                if (depth >= 0)
                {
                    GetFolders(file.FullName, depth);
                }

                if (!CheckFolder)
                {
                    continue;
                }

                if (!NameInRule(file.Name))
                {
                    unMatchPath.Add(file.FullName);
                }
            }
        }

        public bool NameInRule(string name)
        {
            for (int i = 0; i < StartWith.Length; i++)
            {
                if (Regex.Match(name, $"{StartWith[i]}[\\S\\s]+{EndWith[i]}").Success)
                {
                    return true;
                }
            }
            return false;
        }

        public bool NameInRuleTexture(int TextureType,string name)
        {
            if (Regex.Match(name, $"[\\S\\s]+{TextureTypeEnd[TextureType]}").Success)
            {
                return true;
            }
            return false;
        }

        public bool IsNoCheck(string name)
        {
            if(Ignore==null || Ignore.Length>=0)
            {
                return false;
            }

            string namelow = name.ToLower();
            foreach(string ig in Ignore)
            {
                if(namelow.Contains(ig))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
